'use strict';
const Controller = require('egg').Controller;
let obj = {
  id: 2022,
  name: '小红',
  age: 18,
  skills: [ '泰式按摩', '精油搓背', '水疗spa' ],
};

class jspController extends Controller {
  async index() {
    const { ctx } = this;
    await new Promise(resolve => {
      setTimeout(() => {
        resolve(ctx.body = 'hello jsp'
        );
      }, 5000);

    });
  };

  async zy() {
    const { ctx } = this;
    ctx.body = `${ctx.params.name}+${ctx.params.age}`;
  };

  async ejs() {
    const { ctx, app } = this;
    // 打印中间件
    console.log('counter', ctx.session.counter);
    const username = ctx.session.username;
    obj.username = username;
    // obj.currentTime=app.getCurrentTime() //直接使用扩展里面的函数
    obj.currentTime = app.timep; //直接使用扩展里面的函数
    await ctx.render('jsp.html', obj);

  };

  //  对cookie增删改查
  async add() {
    const ctx = this.ctx;
    //服务器设置cookie
    ctx.cookies.set('mimiwanzhi', '黄.com', {
      httpOnly: true,
      encrypt: true
    });
    ctx.session.username = 'jspSession';
    ctx.body = {
      status: 'success',
      data: 'cookie添加成功'
    };
  }

  async del() {
    const ctx = this.ctx;
    ctx.cookies.set('mimiwanzhi', null);
    ctx.session.username = null;
    ctx.body = {
      status: 'success',
      data: 'cookies删除成功'
    };
  }

  async editor() {
    const ctx = this.ctx;
    ctx.cookies.set('mimiwanzhi', 'bilibill');
    ctx.session.username = '技术胖';
    ctx.body = {
      status: 'success',
      data: 'cookies修改成功'
    };
  }

  async show() {
    const ctx = this.ctx;
    const wz = ctx.cookies.get('mimiwanzhi', {
      encrypt: true
    });
    console.log(wz);
    obj.skills.push(wz);

    ctx.body = {
      status: 'success',
      data: 'cookies修改查看成功'
    };

  }

  async newconxtext() {
    const { ctx } = this;
    const params = ctx.params();
    console.log('params', params);
    ctx.body = 'newconxtext';
  }

  async newRequest() {
    const { ctx, params } = this;
    const token = ctx.request.token;// 获得token
    ctx.body = {
      status: 200,
      body: token
    };
  }

  async newResponse() {
    const { ctx, params } = this;
    ctx.response.token = 'jsp';
    ctx.body = 'newResponse';
  }

  async newHelper() {
    const { ctx } = this;
    const testBase64 = ctx.helper.base64Encode('newHelper');
    ctx.body = testBase64;
  }

}

module.exports = jspController;
